package com.example.meituan.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;

import com.example.meituan.activity.bean.CommentBean;
import com.example.meituan.db.DBUntil;

public class CommentDao {

    private static SQLiteDatabase db=DBUntil.con;

    // 添加评论
    public static long addComment(CommentBean comment) {
        ContentValues values = new ContentValues();
        values.put("s_id", comment.getId());
        values.put("s_user_id", comment.getUserId());
        values.put("s_type_id", comment.getTypeId());
        values.put("s_shop_id", comment.getShopId());
        values.put("s_comment", comment.getComment());
        values.put("s_img", comment.getImg());

        return db.insert("d_comment", null, values);
    }

    // 删除评论
    public int deleteCommentById(String id) {
        return db.delete("d_comment", "s_id=?", new String[]{id});
    }

    // 更新评论
    public int updateComment(CommentBean comment) {
        ContentValues values = new ContentValues();
        values.put("s_user_id", comment.getUserId());
        values.put("s_type_id", comment.getTypeId());
        values.put("s_shop_id", comment.getShopId());
        values.put("s_comment", comment.getComment());
        values.put("s_img", comment.getImg());

        return db.update("d_comment", values, "s_id=?", new String[]{comment.getId()});
    }

    // 查询所有评论
    public List<CommentBean> getAllComments() {
        List<CommentBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_comment", null);

        try {
            while (cursor.moveToNext()) {
                CommentBean comment = createCommentFromCursor(cursor);
                list.add(comment);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return list;
    }

    public static List<CommentBean> getAllCommentsByShopId(String shopId) {
        List<CommentBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_comment WHERE s_shop_id=? ", new String[]{shopId});

        try {
            while (cursor.moveToNext()) {
                CommentBean comment = createCommentFromCursor(cursor);
                list.add(comment);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return list;
    }

    // 根据 ID 查询单个评论
    public CommentBean getCommentById(String id) {
        Cursor cursor = db.rawQuery("select * from d_comment where s_id=?", new String[]{id});

        try {
            if (cursor.moveToNext()) {
                return createCommentFromCursor(cursor);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return null;
    }



    // 辅助方法：从游标创建评论对象
    private static CommentBean createCommentFromCursor(Cursor cursor) {
        return new CommentBean(
                cursor.getString(cursor.getColumnIndexOrThrow("s_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_user_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_type_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_shop_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_comment")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_img"))
        );
    }
}